package cn.com.mma.mobile.tracking.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import cn.com.mma.mobile.tracking.api.Global;
import cn.com.mma.mobile.tracking.bean.Argument;
import cn.com.mma.mobile.tracking.bean.Company;
import cn.com.mma.mobile.tracking.bean.Event;
import cn.com.mma.mobile.tracking.bean.SDK;
import cn.com.mma.mobile.tracking.bean.SendEvent;
import cn.mmachina.JniClient;

/**
 * @author Wyi
 * @see 通用工具类
 */
public class CommonUtil {
	/**
	 * 去掉传入的event参数
	 * 
	 * @param url
	 * @param requiredEventValue
	 * @param separator
	 * @param equalizer
	 * @return
	 */
	public static String removeExistEvent(String url, List<String> requiredEventValue, String separator, String equalizer) {
		for (String value : requiredEventValue) {
			if (url.contains(separator + value)) {
				Logger.d("mma_" + separator + value + equalizer + "[^" + separator + "]*");
				url = url.replaceAll(separator + value + equalizer + "[^" + separator + "]*", "");
			}
		}
		return url;
	}

	/**
	 * 获得track分配的track url，主要是去燥：去掉跟拼接参数一样的字段
	 * 
	 * @param url
	 * @param requiredArgmentValue
	 * @param separator
	 * @param equalizer
	 * @param redirectURLValue
	 * @return
	 */
	public static Map removeExistArgmentAndGetRedirectURL(String url, List<String> requiredArgmentValue, String separator, String equalizer,
			String redirectURLValue) {
		requiredArgmentValue = sortByLength(requiredArgmentValue);
		String redirectUrl = "";
		Map<String, String> URLAndRedirectURL = new HashMap<String, String>();
		for (String value : requiredArgmentValue) {
			if (url.contains(separator + value)) {
				if (value.equals(redirectURLValue)) {
					Pattern pattern = Pattern.compile(separator + value + "[^" + separator + "]*");
					Matcher matcher = pattern.matcher(url);
					if (matcher.find())
						redirectUrl = matcher.group(0).replace(separator + value, "");
				}
				url = url.replaceAll(separator + value + equalizer + "[^" + separator + "]*", "");
			}
		}
		URLAndRedirectURL.put(Global.TRACKING_URL, url);
		return URLAndRedirectURL;
	}

	/**
	 * 对集合的内容根据长度进行排序
	 * 
	 * @param list
	 * @return
	 */
	private static List<String> sortByLength(List list) {
		Collections.sort(list, new Comparator<String>() {
			@Override
			public int compare(String s1, String s2) {
				return s1.length() > s2.length() ? -1 : 1;
			}
		});
		return list;
	}

	/**
	 * 根据正则表达式匹配字符串，并返回结果。匹配格式如下：
	 * 原网址：http://c.admaster.com.cn/c/a16239,b200293157,c1851,i0,m101,h
	 * 返回值：http://c.admaster.com.cn
	 * 
	 * 原网址：http://192.168.2.192:8080/untitled3/test/test/a123,c123,b123
	 * 返回值：http://192.168.2.192
	 * 
	 * @param url
	 * @return
	 */
	public static String getHostURL(String url) {
		String hostURL = "";
		Pattern pattern = Pattern.compile("^([\\w\\d]+):\\/\\/([\\w\\d\\-_]+(?:\\.[\\w\\d\\-_]+)*)");
		Matcher matcher = pattern.matcher(url);
		if (matcher.find())
			hostURL = matcher.group(0);
		return hostURL;
	}

	/**
	 * 获得签名
	 * 
	 * @param context
	 * @param url
	 * @return
	 */
	public static String getSignature(Context context, String url) {
		// 检测URL转小写
		url.toLowerCase();
		String signature = JniClient.MDString("", context, url);
		return signature;
	}

	/**
	 * 对String 进行md5加密
	 * 
	 * @param input
	 * @return
	 */
	public static String md5(String input) {
		String result = input;
		if (input != null && !"".equals(input)) {
			MessageDigest md; // or "SHA-1"
			try {
				md = MessageDigest.getInstance("MD5");
				md.update(input.getBytes());
				BigInteger hash = new BigInteger(1, md.digest());
				result = hash.toString(16);
				while (result.length() < 32) {
					result = "0" + result;
				}
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace(); // To change body of catch statement use
										// File | Settings | File Templates.
			}

		}
		return result;
	}

	/**
	 * 判断是否有网络连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetWorkEnable(Context context) {
		boolean result = false;
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		result = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
		return result;
	}

	public static boolean isWifiConnected(Context context) {
		boolean result = false;
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null) {
				result = mWiFiNetworkInfo.isAvailable();
			}
		}
		return result;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isMobileConnected(Context context) {
		boolean result = false;
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null) {
				result = mMobileNetworkInfo.isAvailable();
			}
		}
		return result;
	}

	/**
	 * 判断字符串是否为null，是则返回“”，否则进行utf－8编码后，返回
	 * 
	 * @param str
	 * @return
	 */
	public static String encodingUTF8(String str) {
		try {
			return str == null ? "" : URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	/**
	 * 判断拼接的参数是否需要Encoding
	 * 
	 * @param str
	 * @param argument
	 * @param company
	 * @return
	 */
	public static String encodingUTF8(String str, Argument argument, Company company) {
		try {
			if (company.sswitch.encrypt.containsKey(argument.key)) {
				if ("md5".equals(company.sswitch.encrypt.get(argument.key))) {
					if ("MAC".equals(argument.key)) {
						str = str == null ? "" : str.replaceAll(":", "");
					}
					str = str == null ? "" : CommonUtil.md5(str.toUpperCase());
				}
			}
			if (!argument.urlEncode)
				return str == null ? "" : str;
			else
				return str == null ? "" : URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}
}
